package com.yangxd.hospital.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yangxd.handler.BusinessException;
import com.yangxd.hospital.entity.Department;
import com.yangxd.hospital.entity.User;
import com.yangxd.hospital.service.UserService;
import com.yangxd.response.Result;
import com.yangxd.response.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yangxd
 * @since 2021-04-24
 */
@RestController
@RequestMapping("/hospital/user")
@Api(value="系统用户模块", tags = "系统用户接口")
public class UserController {
    @Autowired
    UserService userService;

    @GetMapping("/chooseDoctor/{departmentId}")
    @ApiOperation(value = "某科室所有医生" , notes = "根据departmentId查询")
    public Result getDoctorByDepartmentId(@PathVariable Integer departmentId) {
            List<User> users = userService.listByDepartmentId(departmentId);
            if(users.size() > 0) {
                return Result.ok().data("doctors",users);
            } else {
                throw new BusinessException(ResultCode.USER_ACCOUNT_NOT_EXIST.getCode(),ResultCode.USER_ACCOUNT_NOT_EXIST.getMessage());
            }
    }

    /**
         * 获取登录的用户信息
     * @return
     */
    @GetMapping("/getUserInfo")
    @ApiOperation(value = "用户信息" , notes = "用户登录信息")
    public Result info() {
        User user = userService.getUserInfo();
        return Result.ok().data("userInfo",user);
    }

    @PostMapping("/updateUser")
    @ApiModelProperty(value = "修改" , notes = "修改用户信息")
    public Result updateUser(@RequestBody User user) {
        try {
            userService.updateUser(user);
            return Result.ok().message("更新成功！");
        }catch (Exception e) {
            return Result.error();

        }
    }

    /**
     * 分页查询所有医生信息
     * @param user
     * @param size
     * @param current
     * @return
     */
    @PostMapping("/getDoctors")
    @ApiOperation(value = "分页查询所有医生信息")
    public Result getDoctors(@RequestBody User user,
                             @RequestParam(required = true,defaultValue = "6") Integer size,
                             @RequestParam(required = true,defaultValue = "1") Integer current) {
        Page<User> page = new Page<>(current,size);
        QueryWrapper<User> queryWrapper = getQueryWrapper(user);
        IPage<User> pageDoctors = userService.findPageDoctors(page, queryWrapper);
        long total = pageDoctors.getTotal();
        final List<User> doctorList = pageDoctors.getRecords();
        if(null != doctorList && doctorList.size() > 0) {
            return Result.ok().data("total",total).data("doctorList",doctorList);
        } else {
            return Result.error();
        }

    }

    @GetMapping("/deleteById/{userId}")
    @ApiOperation(value = "删除医生信息")
    public Result deleteDoctor(@PathVariable Integer userId) {
        try{
            userService.deleteById(userId);
            return Result.ok().message("删除成功！");
        }catch (Exception e){
            return Result.error();
        }

    }

    @PostMapping("/saveUser")
    @ApiOperation(value = "注册用户")
    public Result saveUser(@RequestBody User user) {
        try{
            Integer userId = userService.saveUser(user);
            return Result.ok().data("userId",userId);
        }catch (Exception e) {
            return Result.error();
        }
    }



    private QueryWrapper<User> getQueryWrapper(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (user != null) {
            if (!StringUtils.isEmpty(user.getUserName())) {
                queryWrapper.eq("user_name", user.getUserName());
            }
            if (!StringUtils.isEmpty(user.getType())) {
                queryWrapper.eq("type", user.getType());
            }
            if (!StringUtils.isEmpty(user.getDepartmentName())) {
                queryWrapper.eq("department_name", user.getDepartmentName());
            }
            queryWrapper.eq("deleted",0);
            queryWrapper.isNotNull("department_name");

        }
        return queryWrapper;

    }


}

